package eu.jpereira.trainings.designpatterns.creational.builder;

import java.util.Iterator;

import eu.jpereira.trainings.designpatterns.creational.builder.model.ReportBody;
import eu.jpereira.trainings.designpatterns.creational.builder.model.SaleEntry;
import eu.jpereira.trainings.designpatterns.creational.builder.model.SoldItem;

public class HTMLReportBody implements ReportBody {

	private StringBuffer delegate = new StringBuffer();

	@Override
	public Object getAsString() {
		return this.delegate.toString();
	}

	public void addContent(Object content) {
		this.delegate.append(content);

	}

	@Override
	public void content(ReportBody rb, SaleEntry saleEntry) {

		rb.addContent("<span class=\"customerName\">");
		rb.addContent(saleEntry.getCustomer().getName());
		rb.addContent("</span><span class=\"customerPhone\">");
		rb.addContent(saleEntry.getCustomer().getPhone());
		rb.addContent("</span>");
		rb.addContent("<items>");

	}

	@Override
	public void iterate(ReportBody rb, SaleEntry saleEntry) {

		Iterator<SoldItem> it = saleEntry.getSoldItems().iterator();
		while (it.hasNext()) {
			SoldItem soldEntry = it.next();
			rb.addContent("<item><name>");
			rb.addContent(soldEntry.getName());
			rb.addContent("</name><quantity>");
			rb.addContent(soldEntry.getQuantity());
			rb.addContent("</quantity><price>");
			rb.addContent(soldEntry.getUnitPrice());
			rb.addContent("</price></item>");
		}
		rb.addContent("</items>");

	}

}
